/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare.AdvantagesPanel');
(function() {

    var DQ = Ext.DomQuery;

    /**
    * Display advantages with a customizable panel
    * @class AdvantagesPanel
    * @extends Ext.BoxComponent
    * @namespace net.autodata.compare
    */
    var Panel = Ext.extend(Ext.BoxComponent, {

        autoEl: {
            tag: 'div',
            cls: 'advantages-panel'
        },

        /**
        * @property emptyText
        * @type String
        */
        emptyText: undefined,

        /**
        * @property tbar
        * @type Object
        */
        tbar: undefined,

        /**
        * @property grid
        * @type Object
        */
        grid: undefined,

        /**
        * @property width
        * @type Number
        */
        width: 788,

        constructor: function(cfg) {
            this.addEvents({ 'nodata': true });
            Panel.superclass.constructor.call(this, cfg);
        },

        initComponent: function() {
            Panel.superclass.initComponent.apply(this, arguments);

            var grid = this.getGrid();

            grid.init(this);

            grid.on('dataloaded', this.onDataLoaded, this);

            this.emptyBody = new Ext.BoxComponent({
                autoEl: {
                    tag: 'div',
                    cls: 'advantagespanel-emptybody'
                },
                html: '<div class="empty-text">' + this.emptyText + '</div>',
                hideMode: 'display',
                hidden: true
            });

            this.initTemplates();
        },

        loadToolbar: function() {
            var tb = this.getToolbar();

            if (tb) {
                var store = tb.getStore();
                store.removeAll();

                var view = tb.getView();

                if (view && view.clearSelectedAcode) {
                    view.clearSelectedAcode();
                }

                store.load();
            }
        },

        /**
        * @method initTemplates
        */
        initTemplates: function() {
            var ts = this.templates || {};

            if (!ts.mainTpl) {

                ts.mainTpl = new Ext.XTemplate(
					'<div class="panel {cls}" style="{[this.panelWidth(values.width)]}{[this.panelHeight(values.height)]}">',
					    '<div class="toolbar-wrap"></div>',
						'<div class="grid-wrap"></div>',
					'</div>',
					{
					    panelWidth: function(v) {
					        return Ext.isEmpty(v) ? '' : String.format('width:{0}px;', v);
					    },

					    panelHeight: function(v) {
					        return Ext.isEmpty(v) ? '' : String.format('height:{0}px;', v);
					    },

					    compile: true,
					    disableFormats: true
					}
				);
            }

            this.templates = ts;
        },

        // Public Methods

        setVehicles: function(baseVehicle, competitors) {
            var grid = this.getGrid();
            if (grid.setVehicles) {
                grid.setVehicles(baseVehicle, competitors);
            }
        },

        /**
        * @method getGrid
        * @returns net.autodata.compare.grid.AdvantagesGrid
        */
        getGrid: function() {
            if (!this.grid) {
                this.grid = new net.autodata.compare.grid.AdvantagesGrid(this.gridCfg);
            }

            return this.grid;
        },

        /**
        * @method getToolbar
        * @returns net.autodata.compare.AdvantagesToolbarView
        */
        getToolbar: function() {
            if (!this.toolbar) {
                this.toolbar = new new net.autodata.compare.CompetitorToolbar({
                    view: new net.autodata.compare.AdvantagesToolbarView(),
                    storeCfg: {
                        proxy: new net.autodata.compare.data.CompetitorVehiclesSessionProxy()
                    },
                    plugins: [
                        new net.autodata.compare.plugins.AdvantagesPanelEventBusConnector()
                    ]
                });
            }

            return this.toolbar;
        },

        // Private Event Handlers

        /**
        * @method onDataLoaded
        * @private
        */
        onDataLoaded: function(grid, records) {
            var eb = this.emptyBody;
            if (eb && grid) {
                if (records && records.length > 0) {
                    grid.show();
                    this.emptyBody.hide();
                } else {
                    grid.hide();
                    this.emptyBody.show();
                }
            }
        },

        /**
        * @method onRender
        * @private
        */
        onRender: function() {
            Panel.superclass.onRender.apply(this, arguments);

            this.templates.mainTpl.append(this.el.dom, {
                cls: this.cls || '',
                width: this.width,
                height: this.height
            });

            var grid = this.getGrid();
            grid.hide();
            grid.render(DQ.selectNode('.grid-wrap', this.el.dom));

            var toolbar = this.getToolbar();
            toolbar.render(DQ.selectNode('.toolbar-wrap', this.el.dom));

            this.emptyBody.render(this.el);
        },

        onDestroy: function() {
            if (this.grid) {
                this.grid.un('dataloaded', this.onDataLoaded, this);
            }

            Panel.superclass.onDestroy.apply(this, arguments);
        }
    });

    net.autodata.compare.AdvantagesPanel = Panel;
})();
